package com.orangelife.mobile.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.curry.android.net.NetworkUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.shop.activity.TraderDetailActivity;
import com.orangelife.mobile.util.BLog;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class HomePageListViewAdapter<T> extends FatherAdapter<T> {

	private Context mContext;
	private LayoutInflater listContainer;
	private List<Map<String, Object>> list = null;
	private ImageDownloader image = ImageDownloader.getImageDownloader();

	public final class ListItemView {
		/** 商家名称 */
		public TextView tvName;
		/** 商家内容 */
		public TextView tvContent;
		/** 商家图片 */
		public ImageView ivPic;
		/** 商家类型 */
		public ImageView ivType;
		/** 点击item */
		public RelativeLayout rlHotTraderItem;
	}

	public HomePageListViewAdapter(Context context,
			List<Map<String, Object>> list) {
		this.mContext = context;
		listContainer = LayoutInflater.from(mContext);
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ListItemView listItemView = null;

		if (convertView == null) {
			listItemView = new ListItemView();
			convertView = listContainer.inflate(
					R.layout.item_lv_homepage_trader, null);

			listItemView.tvName = (TextView) convertView
					.findViewById(R.id.tv_name);
			listItemView.tvContent = (TextView) convertView
					.findViewById(R.id.tv_content);
			listItemView.ivPic = (ImageView) convertView
					.findViewById(R.id.iv_trader_photo);
//			listItemView.ivType = (ImageView) convertView
//					.findViewById(R.id.ivType);
			listItemView.rlHotTraderItem = (RelativeLayout) convertView
					.findViewById(R.id.rlHotTraderItem);

			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}


		for (int i = 0; i < list.size(); i++) {
			listItemView.tvName.setText(list.get(position).get("merchantName")
					.toString());
			listItemView.tvContent.setText(list.get(position)
					.get("introduction").toString());
			String category = list.get(position).get("category").toString();
//			if (category.equals("1")) {
//				listItemView.ivType
//						.setBackgroundResource(R.drawable.waimaisongcan);
//			} else if (category.equals("2")) {
//				listItemView.ivType
//						.setBackgroundResource(R.drawable.shenghuozahuo);
//			} else if (category.equals("3")) {
//				listItemView.ivType.setBackgroundResource(R.drawable.jujiafuwu);
//			}
			image.download(list.get(position).get("merchantLogo").toString(),
					listItemView.ivPic,ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
			listItemView.rlHotTraderItem.setOnClickListener(new listener(
					position));
		}
		return convertView;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return HomePageListViewAdapter.this;
	}

	class listener implements View.OnClickListener {

		private Map<String, Object> map = null;
		private int position;

		public listener(int position) {
			this.position = position;
			map = list.get(position);
		}

		@Override
		public void onClick(View view) {
			if (!NetworkUtil.isConnectedNet(mContext)) {
				Toast.makeText(
						mContext,
						mContext.getResources().getString(
								R.string.net_disconnected), Toast.LENGTH_SHORT)
						.show();
				return;
			}
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			intent.putExtra("HotTrader", "hot");
			bundle.putSerializable("detail", (Serializable) map);
			intent.setClass(mContext, TraderDetailActivity.class);
			intent.putExtras(bundle);
			mContext.startActivity(intent);
			if(position<=4){
				try {
					BLog.getInstance().clickAdver(map.get("merID").toString(), 3);
				} catch (Exception e) {
				}
			}
		}

	}
}